﻿using System;
using System.Collections.Generic;
using XCI.Sys.Model;
using XCI.Core;
using XCI.Windows.Extensions;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 系统角色服务
    /// </summary>
    public class RoleService : BaseService<SysRole>
    {
        public static readonly RoleService Instance = new RoleService();
        protected override string RootUrl => "/api/sys/role/";

        /// <summary>
        /// 新建角色模型
        /// </summary>
        public SysRole New()
        {
            return new SysRole
            {
                Id = NewId(),
                Status = true
            };
        }

        /// <summary>
        /// 复制角色模型
        /// </summary>
        /// <param name="original">原模型</param>
        public SysRole Copy(SysRole original)
        {
            if (original == null) throw new ArgumentNullException(nameof(original));
            var model = original?.Clone();
            model.Id = NewId();
            return model;
        }

        /// <summary>
        /// 检查角色主键是否存在
        /// </summary>
        /// <param name="id">角色主键</param>
        public bool ExistById(string id)
        {
            return ExistByIdCore(id);
        }

        /// <summary>
        /// 检查角色编码是否存在
        /// </summary>
        /// <param name="code">角色编码</param>
        public bool ExistByCode(string code)
        {
            return ExistByCodeCore(code);
        }

        /// <summary>
        /// 新建角色
        /// </summary>
        /// <param name="model">角色模型对象</param>
        /// <returns>成功返回true</returns>
        public bool Insert(SysRole model)
        {
            return InsertCore(model);
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="model">角色模型对象</param>
        /// <returns>成功返回true</returns>
        public bool Update(SysRole model)
        {
            return UpdateCore(model);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <returns>成功返回true</returns>
        public bool Delete(string ids)
        {
            return DeleteByIdsCore(ids);
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <param name="status">状态,1启用;0禁用</param>
        /// <returns>成功返回true</returns>
        public bool UpdateStatus(string ids, bool status)
        {
            return UpdateStatusByIdsCore(ids, status);
        }

        /// <summary>
        /// 根据主键获取角色对象
        /// </summary>
        /// <param name="id">角色主键</param>
        public SysRole SelectById(string id)
        {
            return SelectByIdCore(id);
        }

        /// <summary>
        /// 根据角色编码获取角色对象
        /// </summary>
        /// <param name="code">角色编码</param>
        /// <returns>成功返回模型对象</returns>
        public SysRole SelectByCode(string code)
        {
            return SelectByCodeCore(code);
        }

        /// <summary>
        /// 查询角色分页列表
        /// </summary>
        /// <param name="paged">分页参数</param>
        /// <param name="key">编码/名称关键字</param>
        /// <param name="deptId">部门主键</param>
        /// <param name="status">状态(0:禁用;1:启用;null:全部记录)</param>
        public PageList<SysRole> SelectPageList(PageParam paged, string key, string deptId, bool? status)
        {
            var map = GetQueryMap(ToPageMap(paged), key, deptId, status);
            return SelectPageListCore(map);
        }

        /// <summary>
        /// 查询角色列表
        /// </summary>
        /// <param name="key">编码/名称关键字</param>
        /// <param name="deptId">部门主键</param>
        /// <param name="status">状态(0:禁用;1:启用;null:全部记录)</param>
        public List<SysRole> SelectList(string key, string deptId, bool? status)
        {
            var map = GetQueryMap(Map.New, key, deptId, status);
            return SelectListCore(map);
        }

        /// <summary>
        /// 获取指定用户中已关联的角色列表
        /// </summary>
        /// <param name="userId">用户主键</param>
        public List<SysRole> SelectListByUserId(string userId)
        {
            var url = GetUrl("selectListByUserId", userId);
            return SysRuntime.Api.Post<List<SysRole>>(url).EnsureSuccess().Data;
        }

        /// <summary>
        /// 获取指定用户中没有关联的角色列表
        /// </summary>
        /// <param name="userId">用户主键</param>
        public List<SysRole> SelectUnListByUserId(string userId)
        {
            var url = GetUrl("selectUnListByUserId", userId);
            return SysRuntime.Api.Post<List<SysRole>>(url).EnsureSuccess().Data;
        }

        /// <summary>
        /// 导出角色列表
        /// </summary>
        /// <param name="key">编码/名称关键字</param>
        /// <param name="deptId">部门主键</param>
        /// <param name="status">状态(0:禁用;1:启用;null:全部记录)</param>
        public byte[] Export(string key, string deptId, bool? status)
        {
            var map = GetQueryMap(Map.New, key, deptId, status);
            return ExportCore(map);
        }

        private Map GetQueryMap(Map map, string key, string deptId, bool? status)
        {
            return map
                .Add("key", key)
                .Add("deptId", deptId)
                .Add("status", status);
        }
    }
}
